package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

// 设置远程调用的超时时间 500秒
@Service(timeout = 500000)
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Override
    public Long save(Comment comment) {
        //1. 往评论表插入一条数据
        mongoTemplate.insert(comment);
        //2. 修改动态表，修改当前动态的点赞数量
        //2.1 修改条件：动态id
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        //2.2 修改内容：如：likeCount +1
        Update update = new Update();
        // 点赞数量在原有基础上加1    评论类型commentType，1-点赞，2-评论，3-喜欢
        update.inc(comment.getCol(), 1);
        // 修改动态表点赞数量
        mongoTemplate.updateFirst(query, update, Publish.class);
        //3. 查询点赞总数
        query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
        );
        long count = mongoTemplate.count(query, Comment.class);
        return count;
    }
    
    @Override
    public Long delete(Comment comment) {
        //1. 删除评论表点赞数据，删除条件：动态id、commentType、userId
        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
                        .and("userId").is(comment.getUserId())
        );
        mongoTemplate.remove(query, Comment.class);
        
        //2. 修改动态表，比如点赞数据要减去1
        query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(), -1);
        mongoTemplate.updateFirst(query, update, Publish.class);
        
        //3. 查询点赞总数
        query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
        );
        long count = mongoTemplate.count(query, Comment.class);
        return count;
    }
    
    @Override
    public PageResult findByPublishId(String movementId, Integer page, Integer pagesize) {
        // 查询评论列表：db.quanzi_comment.find({publishId:ObjectId('xx'),commentType:2})
        // 构造条件
        Query query = Query.query(
                Criteria.where("publishId").is(new ObjectId(movementId))
                        .and("commentType").is(2)
        );
        query.skip((page - 1) * pagesize).limit(pagesize);
        query.with(Sort.by(Sort.Order.desc("created")));
        // 分页查询
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        // 返回
        return new PageResult(page, pagesize, (int) count, list);
    }
    
    
    /**
     * 消息里查看点赞 评论 列表
     */
    @Override
    public PageResult adviceCommentsList(String publishId, Integer commentType, Integer page, Integer pagesize) {
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(publishId))
                        .and("commentType").is(commentType));
        query.limit(pagesize).skip((page - 1) * pagesize);
        query.with(Sort.by(Sort.Order.desc("created")));
        
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page, pagesize, (int) count, commentList);
        
    }
}
